package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * 购物车服务层实现类
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    /**
     * 新增购物车信息
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        //判断当前新增的菜品或套餐是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        //属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //设置当前的用户ID
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCart(shoppingCart);

        //如果存在则数量 + 1
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //数量 +1、
            shoppingCartList.get(0).setNumber(shoppingCartList.get(0).getNumber() + 1);
            //修改
            shoppingCartMapper.update(shoppingCartList.get(0));
        }else {
            //如果不存在则新增一条数据
            //新增数据的时候判断新增的数据是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //代表新增的是菜品，相反等于 null 则代表新增的是套餐
                //新增菜品的话需要将菜品信息添加到对象中
                Dish dish = dishMapper.selectDishById(dishId);
                //设置菜品名称、图片、金额信息
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //新增套餐的话也需要将套餐信息添加到对象中
                Setmeal setmeal = setMealMapper.selectSetMealById(shoppingCartDTO.getSetmealId());
                //设置套餐名称、图片、金额信息
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //设置加入数量
            shoppingCart.setNumber(1);
            //设置加入时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //调用注册的方法
            shoppingCartMapper.add(shoppingCart);
        }
    }

    /**
     * 通过用户ID获取当前购物车信息
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //设置用户ID
        Long userId = BaseContext.getCurrentId();
        //通过用户ID获取购物车内容
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(userId);
        //返回购物车内容信息
        return shoppingCartList;
    }

    /**
     * 清空当前用户的购物车信息
     */
    @Override
    public void clean() {
        //获取用户ID
        Long userId = BaseContext.getCurrentId();
        //清空用户购物车
        shoppingCartMapper.clean(userId);
    }

    /**
     * 删除当前用户的购物车中的某一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //先获取当前用户ID
        Long userId = BaseContext.getCurrentId();
        //通过给定的数据删除相应的购物车信息
        ShoppingCart shoppingCart = new ShoppingCart();
        //属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //设置用户ID
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //获取当前菜品数量进行判断
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCart(shoppingCart);
        //判断当前集合每一个的数量
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //循环获取购物城信息进行数量递减
            for (ShoppingCart cart : shoppingCartList) {
                if(cart.getNumber() -1 > 0){
                    //如果数量大于 0 则建1
                    cart.setNumber(cart.getNumber() - 1);
                    shoppingCartMapper.update(cart);
                }else {
                    //否则删除
                    shoppingCartMapper.sub(shoppingCart);
                }
            }
        }
    }
}
